package com.nameli.admin.controller;

import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.query.StudentQuery;
import com.nameli.SIM.bean.vo.StudentVO;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.SIM.util.EmptyUtil;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.admin.service.StudentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author NAME-L
 * @Description TODO
 * @className StudentController
 * @date 2021-12-07 14:07:10
 */
@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {
    private static final Log log = LogFactory.getLog(StudentController.class);

    @Autowired
    private StudentService studentService;

    @GetMapping("/index")
    public JsonResult queryAllByPage(@RequestParam(value = "pageIndex") Integer pageIndex ,@RequestParam(value = "pageSize") Integer pageSize){

        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        log.info("enter into -----------> StudentController - queryAllByPage");
        try {
            if (EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)){
                log.info("pageIndex:" + pageIndex + "\t" + "pageSize:"+pageSize);
                log.info("check -----------> pageIndex & pageSize 不为空");
                PageInfo<StudentVO> returnList = studentService.queryAllMajor(pageIndex, pageSize);
                errorCode = ReturnStatus.SUCCESS;
                errorMsg = "查询成功";
                jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnList);
            }else {
                errorCode = ReturnStatus.DO_FAILED;
                errorMsg = "查询失败";
                jsonResult = JsonResult.withoutData(errorCode, errorMsg);
            }
        } catch (Exception e) {
            errorCode = ReturnStatus.ERROR;
            errorMsg = "系统异常";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
            log.error("查询异常："+e);
        }
        return jsonResult;

    }

    @GetMapping("/info/condition")
    public JsonResult queryAllByCondition(StudentQuery studentQuery , @RequestParam(value = "pageIndex") Integer pageIndex ,@RequestParam(value = "pageSize") Integer pageSize){
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息;
        log.info("查询条件：" + studentQuery);
        try {
            if (EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)){
                log.info("pageIndex:" + pageIndex + "\t" + "pageSize:"+pageSize);
                log.info("check -----------> pageIndex & pageSize 不为空");
                PageInfo<StudentVO> returnList = studentService.queryByCondition(studentQuery , pageIndex, pageSize);
                errorCode = ReturnStatus.SUCCESS;
                errorMsg = "查询成功";
                jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnList);
            }else {
                errorCode = ReturnStatus.DO_FAILED;
                errorMsg = "查询失败";
                jsonResult = JsonResult.withoutData(errorCode, errorMsg);
            }
        } catch (Exception e) {
            errorCode = ReturnStatus.ERROR;
            errorMsg = "系统异常";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
            log.error("查询异常："+e);
        }
        return jsonResult;
    }
}
